package com.qiwx.pigeon.helper;


import android.app.Activity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.RadioButton;
import android.widget.TextView;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.widget.SwitchCompat;
import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.avast.android.dialogs.fragment.ListDialogFragment;
import com.avast.android.dialogs.fragment.SimpleDialogFragment;
import com.qiwx.pigeon.R;
import com.qiwx.pigeon.Utils.DateTimeUtils;
import com.qiwx.pigeon.adapter.produce.CageStatusRecordAdapter;
import com.qiwx.pigeon.data.CageStatusConfig;
import com.qiwx.pigeon.data.db.CageStatusRecord;
import com.qiwx.pigeon.data.params.CLZTParam;
import com.qiwx.pigeon.data.params.FertilizeParam;
import com.qiwx.pigeon.data.vo.CageStatusRecordBean;
import com.qiwx.pigeon.data.vo.FertilizeBean;
import com.qiwx.pigeon.widget.CageStatusOptionView;
import com.qiwx.pigeon.widget.CheckEggOptionView;
import com.qiwx.pigeon.widget.itemderection.LineItemDecoration;

import org.litepal.LitePal;

import java.util.List;


//dialog帮助类
public class DialogHelper {
    public final static int CODE_DELETE_WAREHOUSE = 1000;//删除鸽仓
    public final static int CODE_DELETE_CAGES = 1001;//删除鸽笼
    public final static int CODE_SELECT_WAREHOUSE = 1002;//选择仓库

    public static void showCommonDialog(FragmentActivity activity, String message, int requestCode) {
        SimpleDialogFragment.createBuilder(activity, activity.getSupportFragmentManager())
                .setTitle("提示")
                .setMessage(message)
                .setPositiveButtonText("确定")
                .setNegativeButtonText("取消")
                .setRequestCode(requestCode)
                .show();
    }

    public static void showSelectWareHouseDialog(FragmentActivity activity, String[] items) {
        showListDialog(activity, "选择鸽仓", items, CODE_SELECT_WAREHOUSE);
    }

    public static void showListDialog(FragmentActivity activity, String title, String[] items, int requestCode) {
        ListDialogFragment.createBuilder(activity, activity.getSupportFragmentManager())
                .setCancelButtonText("取消")
                .setTitle(title)
                .setRequestCode(requestCode)
                .setItems(items)
                .show();
    }

    //下蛋对话框
    public static void showLayEggDialog(Activity activity, ILayEggInterface iLayEggInterface) {
        View view = activity.getLayoutInflater().inflate(R.layout.dialog_lay_egg, null);
        CheckBox cbBadEgg = view.findViewById(R.id.cb_jd);
        CheckBox cbEmpty = view.findViewById(R.id.cb_empty);


        //是否空窝
        RadioButton rbEmpty = view.findViewById(R.id.rb_cd_empty);

        AlertDialog.Builder builder = new AlertDialog
                .Builder(activity)
                .setView(view)
                .setTitle("下蛋操作")
                .setPositiveButton("确定", ((dialog, which) -> {
                    if (iLayEggInterface != null) {
                        iLayEggInterface.onSelectStatus(cbBadEgg.isChecked(), cbEmpty.isChecked());
                    }
                })).setNegativeButton("取消", ((dialog, which) -> {

                }));
        builder.show();

    }

    //查蛋对话框
    public static void showCheckEggDialog(Activity activity, ICheckEggInterface iCheckEggInterface) {
        View view = activity.getLayoutInflater().inflate(R.layout.dialog_check_egg, null);
        CheckEggOptionView checkEggOptionView = view.findViewById(R.id.check_egg_options);

        AlertDialog.Builder builder = new AlertDialog
                .Builder(activity)
                .setView(view)
                .setTitle("查蛋操作")
                .setPositiveButton("确定", ((dialog, which) -> {
                    if (iCheckEggInterface != null) {
                        iCheckEggInterface.onSelectStatus(checkEggOptionView.getParam(0), 0);
                    }
                })).setNegativeButton("取消", ((dialog, which) -> {

                }));
        builder.show();

    }

    //查崽对话框
    public static void showBabyDialog(Activity activity, ICheckBabyInterface iCheckBabyInterface) {
        View view = activity.getLayoutInflater().inflate(R.layout.dialog_check_baby, null);
//        RadioButton rbChuke1 = view.findViewById(R.id.rb_chuke1);
//        RadioButton rbChuke2 = view.findViewById(R.id.rb_chuke2);
        RadioButton rbChuke3 = view.findViewById(R.id.rb_chuke3);
        RadioButton rbWCK = view.findViewById(R.id.rb_wck);
        RadioButton rbNoEggBaby = view.findViewById(R.id.rb_no_egg_baby);
        SwitchCompat swRemind = view.findViewById(R.id.sw_remind);

        //是否空窝
        RadioButton rbEmpty = view.findViewById(R.id.rb_baby_empty);
        RadioButton rbCZ = view.findViewById(R.id.rb_baby_cz);

        rbChuke3.setOnCheckedChangeListener((btn, ischeck) -> {
            if (ischeck) {
                rbEmpty.setChecked(true);

            }

        });
        rbWCK.setOnCheckedChangeListener((btn, ischeck) -> {

            if (ischeck) {
                rbEmpty.setChecked(true);

            }

        });
        rbNoEggBaby.setOnCheckedChangeListener((btn, ischeck) -> {
            if (ischeck) {
                rbEmpty.setChecked(true);

            }
        });

        AlertDialog.Builder builder = new AlertDialog
                .Builder(activity)
                .setView(view)
                .setTitle("查崽操作")
                .setPositiveButton("确定", ((dialog, which) -> {
                    int babyStatus = 0;
                    int amount = 0;
//                    if (rbChuke1.isChecked()) {
//                        babyStatus = CageStatusConfig.CHECK_BABY_CK1;
//                        amount = 1;
//                    } else if (rbChuke2.isChecked()) {
//                        babyStatus = CageStatusConfig.CHECK_BABY_CK2;
//                        amount = 2;
//                    } else
                    if (rbChuke3.isChecked()) {
                        babyStatus = CageStatusConfig.CHECK_BABY_CK3;
                        amount = 3;
                    } else if (rbWCK.isChecked()) {
                        babyStatus = CageStatusConfig.CHECK_BABY_WCK;
                        amount = 0;
                    } else if (rbNoEggBaby.isChecked()) {
                        babyStatus = CageStatusConfig.CHECK_BABY_NO_EGG_BABY;
                        amount = -1;
                    } else {
                        babyStatus = CageStatusConfig.CHECK_BABY_CK1;
                        amount = 1;
                    }
                    if (iCheckBabyInterface != null) {
                        iCheckBabyInterface.onSelectStatus(rbEmpty.isChecked(), swRemind.isChecked(), babyStatus, amount);
                    }
                })).setNegativeButton("取消", ((dialog, which) -> {

                }));
        builder.show();

    }

    //查蛋对话框
    public static void showCageInfoDialog(Activity activity, FertilizeBean bean, List<CageStatusRecordBean> records, ICheckEggInterface iCheckEggInterface) {
        View view = activity.getLayoutInflater().inflate(R.layout.dialog_show_cage_info, null);
        View titleView = activity.getLayoutInflater().inflate(R.layout.dialog_show_cage_title, null);
        TextView title = titleView.findViewById(R.id.txt_title);
        TextView btn_delete = titleView.findViewById(R.id.btn_delete);
        final AlertDialog mDialog;
        AlertDialog.Builder builder = new AlertDialog
                .Builder(activity)
                .setView(view)
                .setCustomTitle(titleView)
                .setPositiveButton("确定", ((dialog, which) -> {
                    int eggStatus = 0;

                    if (iCheckEggInterface != null) {
                    }
                })).setNegativeButton("取消", ((dialog, which) -> {

                }));
        mDialog = builder.show();
        btn_delete.setOnClickListener(v -> {
            if (mDialog != null) {
                mDialog.dismiss();
            }


            if (iCheckEggInterface != null) {
                iCheckEggInterface.onSelectStatus(null, 0);
            }

        });
        TextView btn_kw = titleView.findViewById(R.id.btn_kw);
        btn_kw.setOnClickListener(v -> {
            if (mDialog != null) {
                mDialog.dismiss();
            }
            if (iCheckEggInterface != null) {
                iCheckEggInterface.onSelectStatus(null, 1);

            }

        });
        title.setText("鸽笼详情");
        TextView txt_date = view.findViewById(R.id.txt_lay_egg_date);
        TextView txt_status = view.findViewById(R.id.txt_produce_status);
        RecyclerView recyclerView = view.findViewById(R.id.recyclerview);
        int c_id = bean.getId();
        String lay_date = bean.getLay_egg_date();
        int status = bean.getProduce_status();
        if (status == CageStatusConfig.PRODUCE_YD
                || status == CageStatusConfig.PRODUCE_FD) {
            btn_kw.setVisibility(View.VISIBLE);
            btn_delete.setVisibility(View.VISIBLE);
        } else {
            btn_delete.setVisibility(View.GONE);
            btn_kw.setVisibility(View.GONE);

        }
        if (status == CageStatusConfig.PRODUCE_KW) {
            txt_date.setText("未开始");
        } else {
            txt_date.setText(lay_date);

        }
        txt_status.setText(CageStatusConfig.getProduceStatusDes(status));


        List<CageStatusRecordBean> data = records;
        if (data != null) {
            recyclerView.setLayoutManager(new LinearLayoutManager(activity));
            recyclerView.addItemDecoration(new LineItemDecoration(1));
            CageStatusRecordAdapter adapter = new CageStatusRecordAdapter(R.layout.adapter_dialog_cage_record_list_item, data);
            recyclerView.setAdapter(adapter);
        }
    }

    //调窝对话框
    public static void showChangeCageDialog(Activity activity, IChangeCageInterface iChangeCageInterface) {
        View view = activity.getLayoutInflater().inflate(R.layout.dialog_change_cage, null);
//        RadioButton rbChuke1 = view.findViewById(R.id.rb_chuke1);
//        RadioButton rbChuke2 = view.findViewById(R.id.rb_chuke2);
        CageStatusOptionView cageStatusOptionView = view.findViewById(R.id.cage_status_option);

        AlertDialog.Builder builder = new AlertDialog
                .Builder(activity)
                .setView(view)
                .setTitle("存栏状态")
                .setPositiveButton("确定", ((dialog, which) -> {
                    CLZTParam param = cageStatusOptionView.getParam();
//
                    if (iChangeCageInterface != null) {
                        iChangeCageInterface.onSelectCage(param);
                    }
                    if (dialog != null) {
                        dialog.dismiss();
                    }
                })).setNegativeButton("取消", ((dialog, which) -> {
                    if (dialog != null) {
                        dialog.dismiss();
                    }
                }));
        builder.show();

    }

    //手动配对成功
    public static void showPDSuccessDialog(Activity activity, IPdSuccessInterface iPdSuccessInterface) {
        View view = activity.getLayoutInflater().inflate(R.layout.dialog_pd_success, null);
        RadioButton rbFd = view.findViewById(R.id.rb_fd);
        RadioButton rbKw = view.findViewById(R.id.rb_kw);
        rbFd.setChecked(true);

        AlertDialog.Builder builder = new AlertDialog
                .Builder(activity)
                .setView(view)
                .setTitle("配对成功")
                .setPositiveButton("确定", ((dialog, which) -> {
                    if (iPdSuccessInterface != null) {
                        iPdSuccessInterface.onSelectCage(rbFd.isChecked(), rbKw.isChecked());
                    }
                    if (dialog != null) {
                        dialog.dismiss();
                    }
                })).setNegativeButton("取消", ((dialog, which) -> {
                    if (dialog != null) {
                        dialog.dismiss();
                    }
                }));
        builder.show();

    }


    public interface ICheckEggInterface {
        //isEmpty  是否空窝
        void onSelectStatus(FertilizeParam param, int methodId);
    }

    public interface ICheckBabyInterface {
        //isEmpty  是否空窝
        void onSelectStatus(boolean isEmpty, boolean isRemind, int status, int amount);
    }

    public interface ILayEggInterface {
        //isEmpty  是否空窝
        void onSelectStatus(boolean isGoodEgg, boolean isEmpty);
    }

    public interface IChangeCageInterface {
        void onSelectCage(CLZTParam param);

    }

    public interface IPdSuccessInterface {
        void onSelectCage(boolean isFd, boolean isKw);

    }


}
